//
//  FeaturedArtistViewController.m
//  Mecca
//
//  Created by Nico Villanueva iOS Dev on 26/8/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//

#import "FeaturedArtistViewController.h"

#import "Constant.h"

#import "PostClass.h"

#import "JsonClass.h"

#import "ParserClass.h"

#import "FeaturedArtistTableViewCell.h"

#import "FeatureArtist.h"

#import "StarRatingView.h"

#import "Artist.h"

#import "UIManager.h"

#import "RotationTabViewController.h"

#import "Album.h"


@interface FeaturedArtistViewController (){
    NSDictionary* json;
    NSArray *searchResults;
}



@end

@implementation FeaturedArtistViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.tableView setBackgroundColor:[UIColor colorWithRed:28.0/255.0 green:28.0/255.0 blue:28.0/255.0 alpha:1.0]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappens:) name:@"cancelFeatureArtist" object:nil]; // Register
    
    self.receivedData = [NSMutableData data];
    NSURLRequest *request = [[PostClass sharedItems] getFeaturedArtist:[UIDevice currentDevice].identifierForVendor.UUIDString];
    _theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    
}


#pragma mark - New Fetch Data;

-(void) somethingHappens:(NSNotification*) notification
{
    NSLog(@"notification = %@",[notification object]);
    
    [self.theConnection cancel];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error data = %@", error);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
    
    if (connection == self.theConnection) {
        
        [_receivedData appendData:data];
        
        //  NSLog(@"data parse = %@", _receivedData);
        
    }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    if (connection == self.theConnection) {
        
        if (!(_receivedData == nil)) {
            
            json = [JsonClass jsonParser:_receivedData];
            
            [[ParserClass sharedItems] getFeaturedArtist:json];
            
            [self.tableView reloadData];
            
            NSLog(@"Featured Artist = %@", json);
            
            FeatureArtist *featuredArtist = [[ParserClass sharedItems] featuredArtist][0];
            
            self.lblArtistName.text = [featuredArtist screenName];
            
        }
        
        NSLog(@"Finish");
    }
    
}



#pragma mark - Other Method;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnBack:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark Table Delegate


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [[[ParserClass sharedItems] featuredArtist] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Configure the cell...
    
    static NSString *cellIdentifier = @"Cell";
    
    FeaturedArtistTableViewCell *cell = (FeaturedArtistTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FeaturedArtistTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:(38/255.0) green:(38/255.0) blue:(38/255.0) alpha:1];
    cell.selectedBackgroundView = selectionColor;
    
    FeatureArtist *featuredArtist = [[ParserClass sharedItems] featuredArtist][indexPath.row];
    
    cell.lblArtistName.text = [featuredArtist screenName];
    cell.lblNumberOfAlbums.text = [NSString stringWithFormat:@"%@ Albums",[featuredArtist albumCount]];

    StarRatingView* starViewNoLabel = [[StarRatingView alloc]initWithFrame:CGRectMake(215, 19, 45, 8) andRating:2 withLabel:NO animated:YES];
    
    [starViewNoLabel setUserInteractionEnabled:NO];
    
    [cell.customCell addSubview:starViewNoLabel];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FeatureArtist *artist = [[[ParserClass sharedItems] featuredArtist] objectAtIndex:indexPath.row];
    
    [[ParserClass sharedItems] getArtistID:[artist artistId]];
    
    [[ParserClass sharedItems] getArtistName:[artist fullName]];
    
    [[ParserClass sharedItems] getArtistImg:[artist profilePicture]];
    
    [self presentViewController:[[UIManager sharedItems] artistTabBars] animated:YES completion:nil];
    
}

@end
